package cn.iocoder.yudao.module.cmt.controller.admin.notify;

import cn.iocoder.yudao.module.cmt.service.notify.CmtNotifyMessageService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.module.cmt.controller.admin.notify.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.notify.CmtNotifyMessageDO;

@Tag(name = "管理后台 - 消息")
@RestController
@RequestMapping("/cmt/notify-message")
@Validated
public class CmtNotifyMessageController {

    @Resource
    private CmtNotifyMessageService messageService;

    @PostMapping("/create")
    @Operation(summary = "创建消息")
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:create')")
    public CommonResult<Long> createMessage(@Valid @RequestBody CmtNotifyMessageSaveReqVO createReqVO) {
        return success(messageService.createMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新消息")
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:update')")
    public CommonResult<Boolean> updateMessage(@Valid @RequestBody CmtNotifyMessageSaveReqVO updateReqVO) {
        messageService.updateMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除消息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:delete')")
    public CommonResult<Boolean> deleteMessage(@RequestParam("id") Long id) {
        messageService.deleteMessage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除消息")
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:delete')")
    public CommonResult<Boolean> deleteMessageList(@RequestParam("ids") List<Long> ids) {
        messageService.deleteMessageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得消息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:query')")
    public CommonResult<CmtNotifyMessageRespVO> getMessage(@RequestParam("id") Long id) {
        CmtNotifyMessageDO message = messageService.getMessage(id);
        return success(BeanUtils.toBean(message, CmtNotifyMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得消息分页")
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:query')")
    public CommonResult<PageResult<CmtNotifyMessageRespVO>> getMessagePage(@Valid CmtNotifyMessagePageReqVO pageReqVO) {
        PageResult<CmtNotifyMessageDO> pageResult = messageService.getMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CmtNotifyMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出消息 Excel")
    @PreAuthorize("@ss.hasPermission('cmt:notify-message:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMessageExcel(@Valid CmtNotifyMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CmtNotifyMessageDO> list = messageService.getMessagePage(pageReqVO).getList();
        ExcelUtils.write(response, "消息.xls", "数据", CmtNotifyMessageRespVO.class,
                        BeanUtils.toBean(list, CmtNotifyMessageRespVO.class));
    }

}
